﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.ComponentModel;
using System.Data;

namespace DAO
{
    public class PhongChieuDAO
    {
        public static List<RapChieu> layDSRap()
        {
            List<RapChieu> dsRap = new List<RapChieu>();
            string sql = "Select * from RapChieu";
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand(sql, cn);
            SqlDataReader datar = cmd.ExecuteReader();
            while (datar.Read())
            {
                RapChieu r = new RapChieu();
                r.MaRap = datar["MaRap"].ToString();
                r.TenRap = datar["TenRap"].ToString();
                r.DiaChi = datar["DiaChi"].ToString();
                dsRap.Add(r);
            }
            cn.Close();
            return dsRap;
        }

        public static List<DinhDangPhim> layDSDieuKienKT()
        {
            List<DinhDangPhim> dsDieuKienKT = new List<DinhDangPhim>();
            string sql = "Select * from DinhDangPhim";
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand(sql, cn);
            SqlDataReader datar = cmd.ExecuteReader();
            while (datar.Read())
            {
                DinhDangPhim dkkt = new DinhDangPhim();
                dkkt.MaDinhDangPhim = (int)datar["MaDinhDangPhim"];
                dkkt.TenDinhDangPhim = datar["TenDinhDangPhim"].ToString();
                dsDieuKienKT.Add(dkkt);
            }
            cn.Close();
            return dsDieuKienKT;
        }

        public static List<TinhTrangPhong> layDSTinhTrangPhong()
        {
            List<TinhTrangPhong> dsTTPhong = new List<TinhTrangPhong>();
            string sql = "Select * from TinhTrangPhong";
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand(sql, cn);
            SqlDataReader datar = cmd.ExecuteReader();
            while (datar.Read())
            {
                TinhTrangPhong dkkt = new TinhTrangPhong();
                dkkt.MaTT = (int)datar["MaTinhTrangPhong"];
                dkkt.TenTT = datar["TenTinhTrangPhong"].ToString();
                dsTTPhong.Add(dkkt);
            }
            cn.Close();
            return dsTTPhong;
        }

        public static BindingList<PhongChieu> getDSPhongByMaRap(string maRap)
        {
            BindingList<PhongChieu> dsPC = new BindingList<PhongChieu>();
            string sql = "Select * from PhongChieu where MaRap=@MaRap";
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Parameters.AddWithValue("@MaRap", maRap);
            SqlDataReader datar = cmd.ExecuteReader();
            while (datar.Read())
            {
                PhongChieu pc = new PhongChieu();
                pc.MaPhong = datar["MaPhongChieu"].ToString();
                pc.TenPhong = datar["TenPhong"].ToString();
                pc.SoDay = (int)datar["SoDayGhe"];
                pc.SoHang = (int)datar["SoHangGhe"];
                pc.MoTaPC = datar["MoTaPhongChieu"].ToString();
                pc.MaRap = datar["MaRap"].ToString();
                pc.MaTT = (int)datar["MaTinhTrangPhong"];
                pc.MaDieuKienKT = (int)datar["DieuKienKyThuat"];
                dsPC.Add(pc);
            }
            cn.Close();
            return dsPC;
        }

        public static int themPhongChieu(PhongChieu pc)
        {
            int kq = -1;
            SqlConnection cn;
            cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_ThemPhongChieu";
            cmd.Connection = cn;

            SqlParameter para;

            para = new SqlParameter("TenPhong", SqlDbType.NVarChar, 20);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.TenPhong;
            cmd.Parameters.Add(para);

            para = new SqlParameter("SoHang", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.SoHang;
            cmd.Parameters.Add(para);

            para = new SqlParameter("SoDay", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.SoDay;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MoTaPC", SqlDbType.NVarChar);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.MoTaPC;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MaRap", SqlDbType.Char,10);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.MaRap;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MaTT", SqlDbType.Char, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.MaTT;
            cmd.Parameters.Add(para);

            para = new SqlParameter("DieuKienKT", SqlDbType.Char, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.MaDieuKienKT;
            cmd.Parameters.Add(para);

            para = new SqlParameter("kq", SqlDbType.Int);
            para.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(para);

            cmd.ExecuteNonQuery();
            kq = (int)cmd.Parameters["kq"].Value;
            cn.Close();

            return kq;
        }

        public static int capNhatPhong(PhongChieu pc)
        {
            int kq = -1;
            SqlConnection cn;
            cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_CapNhatPhong";
            cmd.Connection = cn;

            SqlParameter para;

            para = new SqlParameter("MaPhong", SqlDbType.Char, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.MaPhong;
            cmd.Parameters.Add(para);

            para = new SqlParameter("TenPhong", SqlDbType.NVarChar, 20);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.TenPhong;
            cmd.Parameters.Add(para);

            para = new SqlParameter("SoHang", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.SoHang;
            cmd.Parameters.Add(para);

            para = new SqlParameter("SoDay", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.SoDay;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MoTaPC", SqlDbType.NVarChar);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.MoTaPC;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MaRap", SqlDbType.Char, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.MaRap;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MaTT", SqlDbType.Char, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.MaTT;
            cmd.Parameters.Add(para);

            para = new SqlParameter("DieuKienKT", SqlDbType.Char, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = pc.MaDieuKienKT;
            cmd.Parameters.Add(para);

            para = new SqlParameter("kq", SqlDbType.Int);
            para.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(para);

            cmd.ExecuteNonQuery();
            kq = (int)cmd.Parameters["kq"].Value;
            cn.Close();

            return kq;
        }
        //lấy danh sách phòng trong PhongChieu
        public static DataTable Lay_DS_Phong_THEODINHDANGPHIM(int MDD)
        {
            DataTable dt = new DataTable();
            SqlConnection cn;
            cn = DataProvider.ConnectionData();


            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SP_LOAD_DSPHONG_THEODINHDANGPHIM";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = cn;


            SqlParameter para;

            para = new SqlParameter("MDD", SqlDbType.NVarChar, 20);
            para.Direction = ParameterDirection.Input;
            para.Value = MDD;
            cmd.Parameters.Add(para);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet a = new DataSet();
            da.Fill(a);
            dt = a.Tables[0];
            cn.Close();
            return dt;
        }
    }
}
